<template>
  <div>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="客户编号" prop="customerNo">
        <el-input
          v-model="queryParams.customerNo"
          placeholder="请输入客户编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户渠道">
        <el-select v-model="queryParams.customerFromId"
                   placeholder="请选择"
                   size='mini'>
          <el-option v-for="dict in dict.type.ipms_customer_from"
                     :key="dict.value"
                     :label="dict.label"
                     :value="dict.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="联系人" prop="customerContact">
        <el-input
          v-model="queryParams.customerContact"
          placeholder="请输入联系人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="公司名称" prop="customerCompany">
        <el-input
          v-model="queryParams.customerCompany"
          placeholder="请输入公司名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户类型">
        <el-select v-model="queryParams.customerTypeId"
                   placeholder="请选择"
                   size='mini'>
          <el-option v-for="dict in dict.type.ipms_customer_type"
                     :key="dict.value"
                     :label="dict.label"
                     :value="dict.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="客户状态">
        <el-select v-model="queryParams.customerStatusId"
                   placeholder="请选择"
                   size='mini'>
          <el-option v-for="dict in dict.type.ipms_customer_status"
                     :key="dict.value"
                     :label="dict.label"
                     :value="dict.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="customerList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="客户编号" align="center" prop="customerNo" >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleDetails(scope.row)"
          >{{scope.row.customerNo}}</el-button>
        </template>
      </el-table-column>
      <el-table-column label="客户渠道" align="center" prop="customerFrom"  :formatter="formatFrom" />
      <el-table-column label="联系人" align="center" prop="customerContact" />
      <el-table-column label="联系人电话" align="center" prop="customerPhone" />
      <el-table-column label="联系人邮箱" align="center" prop="customerEmail" />
      <el-table-column label="公司名称" align="center" prop="customerCompany" />
      <el-table-column label="客户单位类型" align="center" prop="customerTypeId" :formatter="formatType"/>
      <el-table-column label="公司地址" align="center" prop="customerAddress" />
      <el-table-column label="客户业务次数" align="center" prop="customerExperience" />
      <el-table-column label="客户状态" align="center" prop="customerStatusId" :formatter="formatStatus"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleCon(scope.row)"
          >合同</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-return"
            @click="handleReturn(scope.row)"
          >回访</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改客户对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-form-item label="客户编号" prop="customerNo">
          <el-input v-model="form.customerNo" placeholder="请输入客户编号" />
        </el-form-item>
        <el-form-item label="客户渠道">
          <el-select v-model="form.customerFromId"
                     placeholder="请选择"
                     size='mini'>
            <el-option v-for="dict in dict.type.ipms_customer_from"
                       :key="dict.value"
                       :label="dict.label"
                       :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="联系人" prop="customerContact">
          <el-input v-model="form.customerContact" placeholder="请输入联系人" />
        </el-form-item>
        <el-form-item label="联系人电话" prop="customerPhone">
          <el-input v-model="form.customerPhone" placeholder="请输入联系人电话" />
        </el-form-item>
        <el-form-item label="联系人邮箱" prop="customerEmail">
          <el-input v-model="form.customerEmail" placeholder="请输入联系人邮箱" />
        </el-form-item>
        <el-form-item label="公司名称" prop="customerCompany">
          <el-input v-model="form.customerCompany" placeholder="请输入公司名称" />
        </el-form-item>
        <el-form-item label="客户单位">
          <el-select v-model="form.customerTypeId"
                     placeholder="请选择"
                     size='mini'>
            <el-option v-for="dict in dict.type.ipms_customer_type"
                       :key="dict.value"
                       :label="dict.label"
                       :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="公司地址" prop="customerAddress">
          <el-input v-model="form.customerAddress" placeholder="请输入公司地址" />
        </el-form-item>
        <el-form-item label="业务经历" prop="customerExperience">
          <el-input v-model="form.customerExperience" placeholder="请输入客户业务经历次数" />
        </el-form-item>
        <el-form-item label="客户状态">
          <el-select v-model="form.customerStatusId"
                     placeholder="请选择"
                     size='mini'>
            <el-option v-for="dict in dict.type.ipms_customer_status"
                       :key="dict.value"
                       :label="dict.label"
                       :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {AllListCustomer, delCustomer, getCustomer, updateCustomer} from "../../../api/crm/crm";

  export default {
    dicts:["ipms_customer_from","ipms_customer_type","ipms_customer_status"],
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 弹出层标题
        title: "",
        // 客户表格数据
        customerList: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          customerNo: null,
          customerContact: null,
          customerPhone: null,
          customerCompany: null,
          customerExperience: null,
          customerTypeId: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
        },
      };
    },

    created(){
      this.getList();
    },
    methods:{
      //格式化字典参数
      formatFrom(row){
        return this.selectDictLabel(this.dict.type.ipms_customer_from,row.customerFromId)
      },
      formatType(row){
        return this.selectDictLabel(this.dict.type.ipms_customer_type,row.customerTypeId)
      },
      formatStatus(row){
        return this.selectDictLabel(this.dict.type.ipms_customer_status,row.customerStatusId)
      },
      getList() {
        this.loading = true;
        AllListCustomer(this.queryParams).then(response => {
          this.customerList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 表单重置
      reset() {
        this.form = {
          customerId: null,
          customerNo: null,
          customerFromId: null,
          customerContact: null,
          customerPhone: null,
          customerEmail: null,
          customerCompany: null,
          customerTypeId: null,
          customerAddress: null,
          userId: null,
          customerExperience: null,
          customerStatusId: null,
          version: null,
          isDelete: null,
          createBy: null,
          createTime: null,
          updateTime: null,
          updateBy: null
        };
        this.resetForm("form");
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
        this.openTwo=false;
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.reset();
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.customerId)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const customerId = row.customerId || this.ids
        getCustomer(customerId).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改客户";
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const customerIds = row.customerId || this.ids;
        this.$modal.confirm('是否确认删除客户编号为"' + customerIds + '"的数据项？').then(function() {
          return delCustomer(customerIds);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('system/customer/export', {
          ...this.queryParams
        }, `customer_${new Date().getTime()}.xlsx`)
      },
      handleDetails(row){
        const customerId = row.customerId || this.ids
        getCustomer(customerId).then(response => {
          this.$router.push({
            path:'/crm/details',
            query:{
              data:response.data
            }
          })
        });
      },
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.customerId != null) {
              console.log(this.form);
              updateCustomer(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addCustomer(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
    }
  }
</script>

<style lang="stylus" scoped>

</style>
